package com.tes.dao;

import java.util.ArrayList;
import java.util.List;

import com.tes.database.DatabaseHelper;
import com.tes.model.HighScore;
import com.tes.tabel.TabelHighScore;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class HighScoreDAO {
	private Context context;

	public HighScoreDAO(Context context) {
		this.context = context;
	}

	public List<HighScore> gets() {
		List<HighScore> result = new ArrayList<HighScore>();

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String kolom[] = new String[] { TabelHighScore.KOLOM_ID,
					TabelHighScore.KOLOM_SCORE };
			Cursor c = db.query(TabelHighScore.NAMA_TABEL, kolom, null, null,
					null, null, null);

			while (c.moveToNext()) {
				HighScore hScore = new HighScore();

				hScore.setLevel(c.getInt(0));
				hScore.setScore(c.getInt(1));

				result.add(hScore);
			}

			c.close();
			db.close();
		} catch (Exception e) {
			result = null;
		}

		return result;
	}

	public HighScore get(int level) {
		HighScore result = new HighScore();

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String kolom[] = new String[] { TabelHighScore.KOLOM_ID,
					TabelHighScore.KOLOM_SCORE };
			String selection = TabelHighScore.KOLOM_ID + " = " + level;
			Cursor c = db.query(TabelHighScore.NAMA_TABEL, kolom, selection,
					null, null, null, null);

			if (c.moveToFirst()) {
				HighScore hScore = new HighScore();

				hScore.setLevel(c.getInt(0));
				hScore.setScore(c.getInt(1));

				result = hScore;
			}

			c.close();
			db.close();
		} catch (Exception e) {
			result = null;
		}

		return result;
	}

	public boolean insertScore(HighScore hScore) {
		boolean result = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();
			ContentValues cv = new ContentValues();

			cv.put(TabelHighScore.KOLOM_ID, hScore.getLevel());
			cv.put(TabelHighScore.KOLOM_SCORE, hScore.getScore());

			result = db.insert(TabelHighScore.NAMA_TABEL, null, cv) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			result = (Boolean) null;
		}

		return result;
	}

	public boolean updateScore(HighScore hScore) {
		boolean result = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();
			ContentValues cv = new ContentValues();

			cv.put(TabelHighScore.KOLOM_SCORE, hScore.getScore());

			String whereClause = TabelHighScore.KOLOM_ID + " = "
					+ hScore.getLevel();
			result = db
					.update(TabelHighScore.NAMA_TABEL, cv, whereClause, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			result = (Boolean) null;
		}

		return result;
	}

	public boolean deleteScore(int id) {
		boolean hasil = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String whereClause = TabelHighScore.KOLOM_ID + " = " + id;
			hasil = db.delete(TabelHighScore.NAMA_TABEL, whereClause, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			hasil = (Boolean) null;
		}

		return hasil;
	}

	public boolean deleteAll() {
		boolean hasil = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			hasil = db.delete(TabelHighScore.NAMA_TABEL, null, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			hasil = (Boolean) null;
		}

		return hasil;
	}
}
